import 'package:flutter/material.dart';
import 'package:easy_refresh/easy_refresh.dart';
import 'package:kq_flutter_core_widget/config/kq_core_global.dart';
import 'package:kq_flutter_core_widget/resources/l10n/kq_core_s.dart';
import 'package:get/get.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:kq_flutter_core_widget/utils/kq_screen_util.dart';

import 'pages/home/homePage/view.dart';
import 'router/route_map.dart';

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  //公共函数初始化后再初始化app
  KqCoreGlobal.init().then((value) => runApp(const MyApp()));
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return KqScreenUtilInit(
      //初始化屏幕适配框架
      designSize: const Size(414, 736),
      minTextAdapt: true,
      builder: _buildApp,
      child: const HomePage(),
    );
  }

  Widget _buildApp(BuildContext context, Widget? child) {
    return GetMaterialApp(
      title: '科强Flutter核心库',
      navigatorKey: KqCoreGlobal.navigatorKey,
      defaultTransition: Transition.rightToLeft,
      getPages: RouteMap.getPages,
      theme: ThemeData(
        useMaterial3: false,
        primarySwatch: Colors.blue,
      ),
      localizationsDelegates: [
        GlobalMaterialLocalizations.delegate,
        GlobalCupertinoLocalizations.delegate,
        GlobalWidgetsLocalizations.delegate,
        KqCoreLocalizationDelegate.delegate,
      ],
      supportedLocales: KqCoreS.supportedLocales,
      //设置默认语言
      fallbackLocale: const Locale('zh'),
      home: const HomePage(),
      builder: (context, child) {
        EasyRefresh.defaultHeaderBuilder = () => const MaterialHeader();
        return EasyLoading.init()(context, child);
      },
    );
  }
}
